<template>
  <div class="app-container">
    <el-row class="row bg-white">
      <el-row>
        <el-button type="primary" class="green-btn m-bt-20" size="small"  style="width: 120px" @click="handleCreate">添加广告版位</el-button>
      </el-row>
      <el-table :data="list"  element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column label='广告版位' align="center">
          <template slot-scope="scope">
            {{scope.row.position_name}}
          </template>
        </el-table-column>
        <el-table-column label="创意形式" align="center">
          <template slot-scope="scope">
            {{scope.row.description}}
          </template>
        </el-table-column>
        <el-table-column label="终端" align="center">
          <template slot-scope="scope">
            {{scope.row.type_str}}
          </template>
        </el-table-column>
        <el-table-column label="长" align="center">
          <template slot-scope="scope">
            {{scope.row.long}}
          </template>
        </el-table-column>
        <el-table-column label="宽" align="center">
          <template slot-scope="scope">
            {{scope.row.width}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" v-if="btnPositionEdit" class="green-btn-min" @click="handleUpdate(scope.row)" >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-row>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible"  width="25%" center>
      <el-form center ref="positionForm" :model="positionForm" :rules="positionRules" label-position="left" label-width="100px">
        <el-input size="small" style="width: 330px;" type="hidden" v-model="positionForm.id"></el-input>
        <el-form-item label="广告版位"  prop="position_name">
          <el-input size="small"  style="width: 330px;" v-model="positionForm.position_name" placeholder="请输入广告版位" ></el-input>
        </el-form-item>
        <el-form-item label="创意形式"  prop="description">
          <el-input size="small" style="width: 330px;" v-model="positionForm.description" placeholder="请输入创意形式" ></el-input>
        </el-form-item>
        <el-form-item label="终端类型"  prop="type">
          <el-select  size="small" style="width: 330px;"  v-model="positionForm.type" filterable placeholder="选择终端类型">
            <el-option v-for="item in  deviceOptions" :key="item.key" :label="item.text" :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告长度"  prop="long">
          <el-input-number v-model="positionForm.long" :min="50" :max="1500" label="长度"></el-input-number>
        </el-form-item>
        <el-form-item label="广告宽度"  prop="width">
          <el-input-number v-model="positionForm.width" :min="50" :max="1500" label="宽度"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" class="green-btn" v-if="dialogStatus=='create'" type="primary"  @click="createData">创建</el-button>
        <el-button size="small" class="green-btn" v-else  @click="updateData" type="primary">修改</el-button>
        <el-button size="small" class="w-130" @click="dialogFormVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { getPostionPagingList, positionSave } from '@/api/assistive'
  import { in_array } from '@/utils/index'

  export default {
    name: 'recharge',
    data() {
      return {
        list: [],
        total: null,
        radio_price: 3,
        deviceOptions: [
          { key: 1, text: '手机' },
          { key: 2, text: 'PC' }
        ],
        listQuery: {
          page: 1,
          pageSize: 10
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑广告版位',
          create: '新建广告版位'
        },
        positionForm: {
          id: '',
          position_name: '',
          description: '',
          long: '',
          width: '',
          type: ''
        },
        positionRules: {
          position_name: [{ required: true, message: '请输入广告版位(2-5字符)', trigger: 'blur' },
            { min: 2, max: 5, message: '请输入广告版位(2-5字符)', trigger: 'blur' }],
          description: [{ required: true, message: '请输入创意形式', trigger: 'blur' },
            { min: 2, max: 15, message: '请输入创意形式(2-15字符)', trigger: 'blur' }],
          long: [{ required: true, message: '请输入广告长度(数值)', type: 'number', trigger: 'blur' }],
          width: [{ required: true, message: '请输入广告宽度(数值)', type: 'number', trigger: 'blur' }],
          type: [{ required: true, message: '请输选择广告类型', trigger: 'change' }]
        },
        btnPositionAdd: false,
        btnPositionEdit: false
      }
    },
    created() {
      this.getButtonStatus()
      this.getList()
    },
    methods: {
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleCreate() {
        this.handleResetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['positionForm'].clearValidate()
        })
      },
      handleUpdate(row) {
        this.positionForm = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['positionForm'].clearValidate()
        })
      },
      handleResetForm() {
        this.positionForm.id = ''
        this.positionForm.position_name = ''
        this.positionForm.description = ''
        this.positionForm.long = ''
        this.positionForm.width = ''
        this.positionForm.type = ''
      },
      getList() {
        getPostionPagingList(this.listQuery).then(response => {
          if (response.code === 1) {
            this.list = response.data.list
            this.total = response.data.total
            setTimeout(() => {
              this.listLoading = false
            }, 1.5 * 1000)
          }
        })
      },
      createData() {
        this.$refs.positionForm.validate(valid => {
          if (valid) {
            positionSave(this.positionForm).then(response => {
              if (response.code === 1) {
                this.$message({
                  title: '成功',
                  message: '创建成功！',
                  type: 'success',
                  duration: 3000
                })
                this.dialogFormVisible = false
                this.getList()
                this.$refs.positionForm.resetFields()
              }
            })
          }
        })
      },
      updateData() {
        positionSave(this.positionForm).then(response => {
          console.log()
          if (response.code === 1) {
            this.$message({
              title: '成功',
              message: '修改成功！',
              type: 'success',
              duration: 3000
            })
            this.dialogFormVisible = false
            this.getList()
            this.$refs.positionForm.resetFields()
          }
        })
      },
      getButtonStatus() {
        const btnList = this.$store.getters.buttons
        if (in_array('positionAdd', btnList)) {
          this.btnPositionAdd = true
        }
        if (in_array('positionEdit', btnList)) {
          this.btnPositionEdit = true
        }
      }
    }
  }
</script>

